package com.aier.proxy.entity.mapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.ProxyIcard;

public class ProxyIcardRowMapper implements RowMapper<ProxyIcard> {

    @SuppressWarnings("unused")
    private int type;

    public ProxyIcardRowMapper() {
    }

    public ProxyIcardRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ProxyIcard mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseProxycard(new ProxyIcard(), rs);
    }

    public static ProxyIcard parseProxycard(ProxyIcard pi, ResultSet rs) throws SQLException {
        return parseProxycard(pi, rs, null);
    }

    public static ProxyIcard parseProxycard(ProxyIcard pi, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            pi.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            pi.setPid(null);
        }
        try {
            pi.setIcid(rs.getLong(su+"icid"));
        } catch (SQLException e) {
            pi.setIcid(null);
        }

        
        return pi;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ProxyIcard pi) {
        args.put("pid",pi.getPid());
        args.put("icid", pi.getIcid());
        return args;
    }
    
    

    @SuppressWarnings("unused")
    private static String parseSQL(ProxyIcard p) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into proxy_icard(");
        int count = 0;
        if (null != p.getPid()) {
            sql.append("pid,");
            count++;
        }
        if (null != p.getIcid()) {
            sql.append("icid,");
            count++;
        }


        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    @SuppressWarnings("unused")
    private static void parsePS(PreparedStatement ps, ProxyIcard p)
        throws SQLException {
        int count = 1;
        if (null != p.getPid()) {
            ps.setInt(count++, p.getPid());
        }
        if (null != p.getIcid()) {
            ps.setLong(count++, p.getIcid());
        }
    }
}
